package com.bang.study.filter;

import com.bang.study.constants.PublicConstants;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.MessageSelector;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @Auther: Bang
 * @Date: 2020/5/6 23:43
 * @Description:
 */
public class SqlConsumer {

    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(PublicConstants.TEST_CONSUMER_GROUP);
        consumer.setNamesrvAddr(PublicConstants.NAME_ADDR);
        consumer.subscribe("FILTER_SQL_TOPIC", MessageSelector.bySql("a >=2"));
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                for (MessageExt msg : msgs) {
                    byte[] body = msg.getBody();
                    String s = new String(body);
                    System.out.println(
                            String.format("当前线程{%s} 内容:{%s} msgId:{%s}",
                                    Thread.currentThread().getName(), s,msg.getMsgId()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
        System.out.println("消费者启动了---------------");
    }
}
